﻿using System.Collections.Generic;
using System.Linq;

namespace Fabrication.ValueProviders
{
    // TODO: Throw exception if not enough generated items to use all values
    internal sealed class AllValueProvider<TParam> : ValueProviderBase<TParam>
    {
        private readonly List<TParam> unusedValues;
        private readonly List<TParam> values;

        public AllValueProvider(IEnumerable<TParam> values)
        {
            this.values = values.ToList();
            unusedValues = values.ToList();
        }

        // TODO: Get cleverer about distributing the unique values (i.e. don't just run through the unused list first)
        protected override TParam GetValue(object item)
        {
            if (unusedValues.Any())
            {
                var unusedIndex = RandomNumberGenerator.GetInteger(0, unusedValues.Count() - 1);
                var unusedValue = unusedValues.ElementAt(unusedIndex);
                unusedValues.RemoveAt(unusedIndex);
                return unusedValue;
            }

            var index = RandomNumberGenerator.GetInteger(0, values.Count() - 1);
            return values.ElementAt(index);
        }
    }
}